package com.haojiang.mh.remote.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.haojiang.mh.R
import com.haojiang.mh.remote.adapter.GroupTypeDialogFragAdapter
import com.haojiang.mh.remote.base.BaseDialogFrag
import com.haojiang.mh.remote.event.AddGroupEvent
import kotlinx.android.synthetic.main.frag_dialog_group_type.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class GroupTypeDialogFrag : BaseDialogFrag() {

    /**
     * 伴生函数
     */
    companion object {
        private val instance by lazy { GroupTypeDialogFrag() }

        fun show(fragmentActivity: FragmentActivity) {
            val fm = fragmentActivity.supportFragmentManager
            fm.executePendingTransactions()
            fm.beginTransaction().remove(instance)
            instance.show(fm, GroupTypeDialogFrag::class.java.simpleName)
        }
    }

    private val groupTypeDialogFragAdapter = GroupTypeDialogFragAdapter()


    override fun onSubCreate(savedInstanceState: Bundle?) {
//        LogUtil.e("创建了onSubCreate")
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.fragment_dialog_fullscreen)
    }


    override fun onSubCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//        LogUtil.e("创建了onSubCreateView")
//        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
//        }
        return inflater.inflate(R.layout.frag_dialog_group_type, container, false)
    }


    override fun onSubViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog!!.setCancelable(true) //是否可以点击取消
        dialog!!.setCanceledOnTouchOutside(true) //dialog外面是否可以点击
//        val params = dialog!!.window!!.attributes
//        val context = context
//        context?.let {
//            params.width = LinearLayout.LayoutParams.MATCH_PARENT
//            params.height = LinearLayout.LayoutParams.MATCH_PARENT
//            params.gravity = Gravity.BOTTOM
//            //高度自己定义
//            dialog!!.window!!.setLayout(params.width, params.height)
//        }


//        groupTypeDialogFragAdapter.setDataList()
    }


    override fun onSubActivityCreated(savedInstanceState: Bundle?) {
//        bleDialogFragAdapter.iOnBleItemClickLisenter = iOnBleItemClickLisenter
//        recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
//        recyclerView.layoutManager = LinearLayoutManager(context)
//        recyclerView.itemAnimator = null
//        recyclerView.adapter = bleDialogFragAdapter
        groupTypeDialogFragAdapter.clearData()
        rvGroupType.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        rvGroupType.layoutManager = LinearLayoutManager(context)
        rvGroupType.adapter = groupTypeDialogFragAdapter
        ivBack.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                this@GroupTypeDialogFrag.dismiss()
            }
        })

        btnNext.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                activity?.let { GroupNameDialogFrag.show(it, groupTypeDialogFragAdapter.curGroupType) }
            }
        })
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAddGroupKeyEvent(event: AddGroupEvent) {
        this.dismiss()
    }


    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }

}